package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("user/shoppingCart")
@Api(tags = "用户购物车相关接口")
@Slf4j
public class ShopCarController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加到购物车
     * @param shoppingCartDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加到购物车")
    public Result addDish(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("向购物车添加的菜的数据：{}",shoppingCartDTO);
        shoppingCartService.addDish(shoppingCartDTO);
        return Result.success();
    }

    /**
     * 查询购物车数据
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查看购物车内容")
    public Result<List<ShoppingCart>> selectShoppingCart(){
        List<ShoppingCart> list = shoppingCartService.showShoppingCart();
        return Result.success(list);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    @ApiOperation("清空购物车")
    public Result cleanCart(){
        shoppingCartService.cleanCart();
        return Result.success();
    }

    /**
     * 删除购物车中的一个菜
     * @param shoppingCartDTO
     * @return
     */
    @PostMapping("/sub")
    @ApiOperation("删除购物车中的一个菜")
    public Result subCart(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("删除的菜为：{}",shoppingCartDTO);
        shoppingCartService.delete(shoppingCartDTO);
        return Result.success();
    }
}
